import axios,{InternalAxiosRequestConfig,AxiosResponse,AxiosError} from "axios";
import {message} from 'antd'
import * as storage from '@/utils/storage'
import * as constant from '@/utils/constant'
const service=axios.create({
    baseURL:process.env.REACT_APP_URL,
    timeout:5000
})
//请求拦截器的设置
service.interceptors.request.use((config:InternalAxiosRequestConfig)=>{
    config.headers['Content-Type']="application/x-www-form-urlencoded"
    const token=storage.getStorage('token')
    if(token){
        config.headers['Authentication']=token
    }
    return config
},(err:AxiosError)=>{
    return Promise.reject(err)
})

//响应拦截器
service.interceptors.response.use((response:AxiosResponse)=>{
    return response
},(err:AxiosError)=>{
    if(err.response?.status){
        switch(err.response.status){
            case constant.SERVER_ERR:
                message.warning(constant.SERVER_ERR_MSG)
                break;
            case constant.NOTFOUND_ERR:
                message.warning(constant.NOTFOUND_ERR_MSG)
                break
            case constant.TOKEN_EXPIRE_ERR:
                message.warning(constant.TOKEN_EXPIRE_ERR_MSG)
                break
            default:
                message.warning(constant.DEFAULT_ERR)
                break
        }
    }
})
export default service

